package com.example.book_libapp;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.database.library.UserFunctions;

public class Passforg extends Activity {

	private EditText edEmail;
	private Button btnforpass;
	private TextView msjinformation;

	// JSON Response node names
	private static String KEY_STAT = "stat";
	private static String KEY_ERROR = "code";
	private static String KEY_ERROR_MSG = "message";

	public void onSearch(View v) {
		onSearchRequested();
	}

	public void onHome(View v) {
		return2Home(this);
	}

	public void return2Home(Context context) {
		final Intent intent = new Intent(context, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.passforg);
		edEmail = (EditText) findViewById(R.id.forgEditEmail);
		btnforpass = (Button) findViewById(R.id.forgSubmit);
		msjinformation = (TextView) findViewById(R.id.description);
		btnforpass.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String Email = edEmail.getText().toString();
				msjinformation.setText("");
				CheckInputValue checkvalue = new CheckInputValue();
				if (checkvalue.validateEmail(Email)) {
					UserFunctions userFunction = new UserFunctions();
					JSONObject json = userFunction.forgotPassword(Email);
					Log.e("JSON -- ", json.toString());
					// check for forgot password response
					try {
						if (json.getString(KEY_STAT).equals("ok")) {

							msjinformation
									.setText(R.string.forgotpassword_success);
						} else {
							String code = json.getString(KEY_ERROR);
							// Error in forgot password
							msjinformation
									.setTextColor(R.string.forgotpassword_error1_color); //

							msjinformation.setText(json
									.getString(KEY_ERROR_MSG));

						}

					} catch (JSONException e) {
						e.printStackTrace();
					}

				} else {
					msjinformation.setText(0x7f050035);
				}
			}
		});

	}

}
